package com.scrcu.cloud.wechat.home.endpoint.common.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @auther: siliang liu
 * @date: 2023/12/25 9:52
 * @description: redis操作工具类
 * @version: 1.0.0
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 自增并返回
     *
     * @param key  key
     * @param step 步长
     * @return long
     */
    public Long incr(String key, long step) {
        return redisTemplate.opsForValue().increment(key, step);
    }

    /**
     * 设置缓存过期时间
     *
     * @param key  key
     * @param time 时间
     */
    public void expire(String key, long time) {
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    /**
     * 存值
     *
     * @param key key
     */
    public void set(String key, Object value, long expire) {
        redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
    }

    /**
     * 删除数据
     *
     * @param key key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 判断key是否存在
     *
     * @param key key
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
}
